package com.yuexin.project.statistic.aroundDinner.controller;


import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuexin.common.utils.poi.ExcelUtil;
import com.yuexin.common.utils.security.ShiroUtils;
import com.yuexin.framework.aspectj.lang.annotation.Log;
import com.yuexin.framework.aspectj.lang.enums.BusinessType;
import com.yuexin.framework.web.controller.BaseController;
import com.yuexin.framework.web.domain.AjaxResult;
import com.yuexin.framework.web.page.TableDataInfo;
import com.yuexin.project.statistic.aroundDinner.domain.AroundDinner;
import com.yuexin.project.statistic.aroundDinner.mapper.AroundDinnerMapper;
import com.yuexin.project.statistic.aroundDinner.service.IAroundDinnerService;

/**
 * 围餐明细 信息操作处理
 * 
 * @author yuexin
 * @date 2018-10-17
 */
@Controller
@RequestMapping("/statistic/aroundDinner")
public class AroundDinnerController extends BaseController
{
    private String prefix = "statistic/aroundDinner";
	
	@Autowired
	private IAroundDinnerService aroundDinnerService;
	
	@Autowired
	private AroundDinnerMapper aroundDinnerMapper;
	
	@RequiresPermissions("statistic:aroundDinner:view")
	@GetMapping()
	public String aroundDinner()
	{
	    return prefix + "/aroundDinner";
	}
	
	/**
	 * 查询围餐明细列表
	 */
	@RequiresPermissions("statistic:aroundDinner:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(AroundDinner aroundDinner)
	{
		startPage();
		aroundDinner.setCompanyId(ShiroUtils.getCompanyId());
        List<AroundDinner> list = aroundDinnerService.selectAroundDinnerList(aroundDinner);
		return getDataTable(list);
	}
	
	/**
	 * 新增围餐明细
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存围餐明细
	 */
	@RequiresPermissions("statistic:aroundDinner:add")
	@Log(title = "围餐明细", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(AroundDinner aroundDinner)
	{		
		aroundDinner.setCompanyId(ShiroUtils.getCompanyId());
		return toAjax(aroundDinnerService.insertAroundDinner(aroundDinner));
	}

	/**
	 * 修改围餐明细
	 */
	@GetMapping("/edit/{aroundId}")
	public String edit(@PathVariable("aroundId") Integer aroundId, ModelMap mmap)
	{
		AroundDinner aroundDinner = aroundDinnerService.selectAroundDinnerById(aroundId);
		mmap.put("aroundDinner", aroundDinner);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存围餐明细
	 */
	@RequiresPermissions("statistic:aroundDinner:edit")
	@Log(title = "围餐明细", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(AroundDinner aroundDinner)
	{		
		return toAjax(aroundDinnerService.updateAroundDinner(aroundDinner));
	}
	
	/**
	 * 删除围餐明细
	 */
	@RequiresPermissions("statistic:aroundDinner:remove")
	@Log(title = "围餐明细", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(aroundDinnerService.deleteAroundDinnerByIds(ids));
	}
	
	/**
	 * 导出 exportMealCount
	 */
	@Log(title = "列表", businessType = BusinessType.EXPORT)
    @RequiresPermissions("statistic:aroundDinner:export")
    @PostMapping("/exportAroundDinner")
    @ResponseBody
    public AjaxResult export(AroundDinner aroundDinner)
    {
		aroundDinner.setCompanyId(ShiroUtils.getCompanyId());
        List<AroundDinner> list = aroundDinnerMapper.selectAroundDinnerListForExport(aroundDinner);
        SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd"); 
        SimpleDateFormat sDateFormatHS=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
        for(AroundDinner b : list){
        	String date=sDateFormat.format(b.getAroundDate());
        	b.setAroundDateExcel(date);
        	if(b.getAroundStartDate() != null){
        		String signTime = sDateFormatHS.format(b.getAroundStartDate());
            	b.setAroundStartDateExcel(signTime);
        	}
        }
        ExcelUtil<AroundDinner> util = new ExcelUtil<AroundDinner>(AroundDinner.class);
        return util.exportExcel(list, "Around");
    }
	
}
